//
//  ViewController.m
//  扫码功能
//
//  Created by 李煜 on 2024/4/10.
//

#import "ViewController.h"
#import "AVFoundation/AVFoundation.h"
@interface ViewController ()

@property(nonatomic,strong) AVCaptureDevice * avDevice;
@property(nonatomic,strong) AVCaptureDeviceInput * avInput;
@property(nonatomic,strong) AVCaptureMetadataOutput * avOutput;
@property(nonatomic,strong) AVCaptureSession * avSession;
@property(nonatomic,strong) AVCaptureVideoPreviewLayer * showPeviewLayer;
@property(nonatomic,strong) AVCaptureVideoPreviewLayer * avPreviewLayer;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self checkCameraAuthorizationStatus:^(BOOL granted) {
        
    }];
}
/** 校验是否有相机权限 */
- (void)checkCameraAuthorizationStatus:(void(^)(BOOL granted))permissionGranted
{
    AVAuthorizationStatus videoAuthStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    
    switch (videoAuthStatus) {
            // 已授权
        case AVAuthorizationStatusAuthorized:
        {
            permissionGranted(YES);
        }
            break;
            // 未询问用户是否授权
        case AVAuthorizationStatusNotDetermined:
        {
            // 提示用户授权
            [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
                permissionGranted(granted);
            }];
        }
            break;
            // 用户拒绝授权或权限受限
        case AVAuthorizationStatusRestricted:
        case AVAuthorizationStatusDenied:
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"请在”设置-隐私-相机”选项中，允许访问你的相机" message:nil delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            permissionGranted(NO);
        }
            break;
        default:
            break;
    }
}

/**
 设置相机
*/
- (void)setupCamera {
    self.avDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    self.avInput = [AVCaptureDeviceInput deviceInputWithDevice:self.avDevice error:nil];
    self.avOutput = [[AVCaptureMetadataOutput alloc]init];
    [self.avOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
    
    self.avSession = [[AVCaptureSession alloc]init];
    [self.avSession setSessionPreset:AVCaptureSessionPresetHigh];
    if ([self.avSession canAddInput:self.avInput]){
        [self.avSession addInput:self.avInput];
    }
    if ([self.avSession canAddOutput:self.avOutput]){
        [self.avSession addOutput:self.avOutput];
    }
    
    // 二维码类型 AVMetadataObjectTypeQRCode
    if ([self.avOutput.availableMetadataObjectTypes containsObject:AVMetadataObjectTypeQRCode]){
        self.avOutput.metadataObjectTypes = [NSArray arrayWithObject:AVMetadataObjectTypeQRCode];
        // AVMetadataObjectTypeAztecCode 条形码
    } else{
        return ;
    }
    
    [self.avSession startRunning];//开始扫描
}


/**
 显示的预览layer
 
 @return AVCaptureVideoPreviewLayer
 */
- (AVCaptureVideoPreviewLayer *)showPeviewLayer {
    self.avPreviewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.avSession];
    self.avPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
    return self.avPreviewLayer;
}


/**
 开始扫描
 */
- (void)startRunning {
    [self.avSession startRunning];
}

/**
 结束扫描
 */
- (void)stopRunning {
    [self.avSession stopRunning];
}



@end
